﻿using System;
using System.Windows.Forms;
using Base;
using Core.Base;
using System.ComponentModel;
using System.Collections.Generic;
using Components;
using System.Text;

namespace Core.Component
{
    public class SGVLookUpColumn : DataGridViewColumn, IBindingControl, ISGVControl
    {
        public SGVLookUpColumn()
            : base(new LookUpCell())
        {
        }

        BaseEntity entity;
        BaseModule module;
        #region IBindingControl
        String _bindingPropertyName = "ID";

        String _bindingTable;
        String _bindingColumn;
        String _dataSourceTable;
        String _dataSourceDisplayColumn;
        String _dataSourceKeyColumn;
        String _dataSourceColumns;

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        string IBindingControl.BindingPropertyName
        {
            get
            {
                return _bindingPropertyName;
            }
            set
            {
                _bindingPropertyName = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingTable
        {
            get
            {
                return _bindingTable;
            }
            set
            {
                _bindingTable = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string BindingColumn
        {
            get
            {
                return _bindingColumn;
            }
            set
            {
                _bindingColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceTable
        {
            get
            {
                return _dataSourceTable;
            }
            set
            {
                _dataSourceTable = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceDisplayColumn
        {
            get
            {
                return _dataSourceDisplayColumn;
            }
            set
            {
                _dataSourceDisplayColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public string DataSourceKeyColumn
        {
            get
            {
                return _dataSourceKeyColumn;
            }
            set
            {
                _dataSourceKeyColumn = value;
            }
        }

        [CategoryAttribute(".SGV"), DescriptionAttribute("")]
        public String DataSourceColumns
        {
            get
            {
                return _dataSourceColumns;
            }
            set
            {
                _dataSourceColumns = value;
            }
        }



        #endregion

        public override DataGridViewCell CellTemplate
        {
            get
            {
                return base.CellTemplate;
            }
            set
            {
                // Ensure that the cell used for the template is a CalendarCell.
                if (value != null &&
                    !value.GetType().IsAssignableFrom(typeof(LookUpCell)))
                {
                    throw new InvalidCastException("Must be a CalendarCell");
                }
                base.CellTemplate = value;
            }
        }

        public string SGVTag
        {
            get
            {
                return String.Empty;
            }
            set
            {
            }
        }

        public string SGVShortcutString
        {
            get
            {
                return String.Empty;
            }
            set
            {
            }
        }

        public void RegisterModule(Core.Base.BaseModule _module)
        {
            if (_module != null)
            {
                this.module = _module;
                this.entity = _module.entity;
            }
        }

        public void GeneratePropertyName()
        {

            String strBindingColumnTag = String.Empty;
            String strBindingTableTag = String.Empty;
            String strDataSourceDiplayColumnTag = String.Empty;
            String strDataSourceKeyColumnTag = String.Empty;
            String strDataSourceTableTag = String.Empty;

            if (String.IsNullOrEmpty(BindingColumn) == false)
                strBindingColumnTag = String.Format("{0}:{1}", "BindingColumn", BindingColumn);
            if (String.IsNullOrEmpty(BindingTable) == false)
                strBindingTableTag = String.Format("{0}:{1}", "BindingTable", BindingTable);
            if (String.IsNullOrEmpty(DataSourceDisplayColumn) == false)
                strDataSourceDiplayColumnTag = String.Format("{0}:{1}", "DataSourceDisplayColumn", DataSourceDisplayColumn);
            if (String.IsNullOrEmpty(DataSourceKeyColumn) == false)
                strDataSourceKeyColumnTag = String.Format("{0}:{1}", "DataSourceKeyColumn", DataSourceKeyColumn);
            if (String.IsNullOrEmpty(DataSourceTable) == false)
                strDataSourceTableTag = String.Format("{0}:{1}", "DataSourceTable", DataSourceTable);

            StringBuilder strTagBuilder = new StringBuilder();

            if (String.IsNullOrEmpty(strBindingColumnTag) == false)
                strTagBuilder.Append(String.Format("{0}|", strBindingColumnTag));
            if (String.IsNullOrEmpty(strBindingTableTag) == false)
                strTagBuilder.Append(String.Format("{0}|", strBindingTableTag));
            if (String.IsNullOrEmpty(strDataSourceDiplayColumnTag) == false)
                strTagBuilder.Append(String.Format("{0}|", strDataSourceDiplayColumnTag));
            if (String.IsNullOrEmpty(strDataSourceKeyColumnTag) == false)
                strTagBuilder.Append(String.Format("{0}|", strDataSourceKeyColumnTag));
            if (String.IsNullOrEmpty(strDataSourceTableTag) == false)
                strTagBuilder.Append(String.Format("{0}|", strDataSourceTableTag));

            DataPropertyName = strTagBuilder.ToString();

        }

        public void DestrucPropertyName()
        {

            if (String.IsNullOrEmpty(DataPropertyName)) return;
            String strColumnTag = DataPropertyName;
            DataPropertyName = String.Empty;

            String[] lstTags = strColumnTag.Split('|');

            if (lstTags.Length >= 1)
                DataPropertyName = lstTags[0];
            if (lstTags.Length >= 2)
                BindingTable = lstTags[1];
            if (lstTags.Length >= 3)
                DataSourceDisplayColumn = lstTags[2];
            if (lstTags.Length >= 4)
                DataSourceKeyColumn = lstTags[3];
            if (lstTags.Length >= 5)
                DataSourceTable = lstTags[4];

        }

        public void Init()
        {
            //DestrucPropertyName();
            //InitDataSource();
            //this.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft;
            ////this.DefaultCellStyle.Font = new System.Drawing.Font(this.DefaultCellStyle.Font.FontFamily, this.DefaultCellStyle.Font.Size, System.Drawing.FontStyle.Bold);
        }

        public virtual void InitDataSource()
        {
            try
            {
               
            }
            catch (Exception ex)
            {

            }


            //this.Columns
        }

        public void UpdateFormat()
        {
        }
    }

    public class LookUpCell : DataGridViewTextBoxCell
    {

        public LookUpCell()
            : base()
        {
            // Use the short date format.
           // this.Style.Format = "d";
        }

        public override void InitializeEditingControl(int rowIndex, object
            initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            // Set the value of the editing control to the current cell value.
            base.InitializeEditingControl(rowIndex, initialFormattedValue,
                dataGridViewCellStyle);
            LookUpEditingControl ctl =
                DataGridView.EditingControl as LookUpEditingControl;
            //ctl.Value = (DateTime)this.Value;
            ctl.ID = Convert.ToInt32(this.Value);
        }

        public override Type EditType
        {
            get
            {
                // Return the type of the editing contol that CalendarCell uses.
                return typeof(LookUpEditingControl);
            }
        }

        public override Type ValueType
        {
            get
            {
                // Return the type of the value that CalendarCell contains.
                return typeof(Int32);
            }
        }

        public override object DefaultNewRowValue
        {
            get
            {
                // Use the current date and time as the default value.
                //return DateTime.Now;
                return 0;
            }
        }
    }

    class LookUpEditingControl : SGVTextBoxLookUp, IDataGridViewEditingControl
    {
        DataGridView dataGridView;
        private bool valueChanged = false;
        int rowIndex;

        public LookUpEditingControl()
            :base()
        {
            //this.Format = DateTimePickerFormat.Short;
            this.DataSourceTable = "ICProducts";
            this.DataSourceKeyColumn = "ID";
            this.DataSourceDisplayColumn = "Name";
            this.DataSourceColumns = "No|Name";
            this.AllowDrill = true;
            this.CausesValidation = false;
            this.Init();

        }

        // Implements the IDataGridViewEditingControl.EditingControlFormattedValue 
        // property.
        public object EditingControlFormattedValue
        {
            get
            {
                //return this.Value.ToShortDateString();
                //return this.ID;
                return this.Text;
            }
            set
            {
                //this.Value = DateTime.Parse((String)value);
                //this.ID = (Int32)value;
                SelectItemText();
            }
        }

        // Implements the 
        // IDataGridViewEditingControl.GetEditingControlFormattedValue method.
        public object GetEditingControlFormattedValue(
            DataGridViewDataErrorContexts context)
        {
            return EditingControlFormattedValue;
        }

        // Implements the 
        // IDataGridViewEditingControl.ApplyCellStyleToEditingControl method.
        public void ApplyCellStyleToEditingControl(
            DataGridViewCellStyle dataGridViewCellStyle)
        {
            this.Font = dataGridViewCellStyle.Font;
            //this.CalendarForeColor = dataGridViewCellStyle.ForeColor;
            //this.CalendarMonthBackground = dataGridViewCellStyle.BackColor;
        }

        // Implements the IDataGridViewEditingControl.EditingControlRowIndex 
        // property.
        public int EditingControlRowIndex
        {
            get
            {
                return rowIndex;
            }
            set
            {
                rowIndex = value;
            }
        }

        // Implements the IDataGridViewEditingControl.EditingControlWantsInputKey 
        // method.
        public bool EditingControlWantsInputKey(
            Keys key, bool dataGridViewWantsInputKey)
        {
            // Let the DateTimePicker handle the keys listed.
            switch (key & Keys.KeyCode)
            {
                case Keys.Left:
                case Keys.Up:
                case Keys.Down:
                case Keys.Right:
                case Keys.Home:
                case Keys.End:
                case Keys.PageDown:
                case Keys.PageUp:
                    return true;
                default:
                    return false;
            }
        }

        // Implements the IDataGridViewEditingControl.PrepareEditingControlForEdit 
        // method.
        public void PrepareEditingControlForEdit(bool selectAll)
        {
            // No preparation needs to be done.
        }

        // Implements the IDataGridViewEditingControl
        // .RepositionEditingControlOnValueChange property.
        public bool RepositionEditingControlOnValueChange
        {
            get
            {
                return true;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingControlDataGridView property.
        public DataGridView EditingControlDataGridView
        {
            get
            {
                return dataGridView;
            }
            set
            {
                dataGridView = value;
                if (dataGridView is SGVGridView)
                {
                    RegisterModule(((SGVGridView)dataGridView).module);
                }
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingControlValueChanged property.
        public bool EditingControlValueChanged
        {
            get
            {
                return valueChanged;
            }
            set
            {
                valueChanged = value;
            }
        }

        // Implements the IDataGridViewEditingControl
        // .EditingPanelCursor property.
        public Cursor EditingPanelCursor
        {
            get
            {
                return base.Cursor;
            }
        }

        protected override void OnTextChanged(EventArgs e)
        {
            //Notify the DataGridView that the contents of the cell
            //have changed.
            valueChanged = true;
            this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
            base.OnTextChanged(e);

        }

        //protected override void OnValueChanged(EventArgs eventargs)
        //{
        //    // Notify the DataGridView that the contents of the cell
        //    // have changed.
        //    valueChanged = true;
        //    this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
        //    //this.textBox1.On
        //    //base.OnValueChanged(eventargs);
        //}
    }

}
